package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SparePartsInventory;
import com.ruoyi.system.domain.UsageRecords;
import com.ruoyi.system.mapper.UsageRecordsMapper;
import com.ruoyi.system.service.ISparePartsInventoryService;
import com.ruoyi.system.service.IUsageRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 备件使用记录管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class UsageRecordsServiceImpl implements IUsageRecordsService {
    @Autowired
    private UsageRecordsMapper usageRecordsMapper;
    @Autowired
    private ISparePartsInventoryService service;

    /**
     * 查询备件使用记录管理
     *
     * @param recordId 备件使用记录管理主键
     * @return 备件使用记录管理
     */
    @Override
    public UsageRecords selectUsageRecordsByRecordId(Long recordId) {
        return usageRecordsMapper.selectUsageRecordsByRecordId(recordId);
    }

    /**
     * 查询备件使用记录管理列表
     *
     * @param usageRecords 备件使用记录管理
     * @return 备件使用记录管理
     */
    @Override
    public List<UsageRecords> selectUsageRecordsList(UsageRecords usageRecords) {
        return usageRecordsMapper.selectUsageRecordsList(usageRecords);
    }

    /**
     * 新增备件使用记录管理
     *
     * @param usageRecords 备件使用记录管理
     * @return 结果
     */
    @Override
    public int insertUsageRecords(UsageRecords usageRecords) {
        usageRecords.setCreateTime(DateUtils.getNowDate());
        usageRecords.setUsageTime(DateUtils.getNowDate());
        //
        SparePartsInventory sparePartsInventory = new SparePartsInventory();
        sparePartsInventory.setPartName(usageRecords.getSparePartName());
        List<SparePartsInventory> sparePartsInventories = service.selectSparePartsInventoryLists(sparePartsInventory);

        Integer num1 = Integer.valueOf(usageRecords.getQuantityUsed().toString());
        for (SparePartsInventory partsInventory : sparePartsInventories) {
            if (num1 > 0) {
                if (num1 > partsInventory.getStockQuantity()) {
                    num1 = Math.toIntExact(num1 - partsInventory.getStockQuantity());
                    partsInventory.setStockQuantity(new Long(0));
                    service.updateSparePartsInventory(partsInventory);
                } else {

                    partsInventory.setStockQuantity(partsInventory.getStockQuantity() - num1);
                    num1 = 0;
                    service.updateSparePartsInventory(partsInventory);
                }
            }


        }
        return usageRecordsMapper.insertUsageRecords(usageRecords);
    }

    /**
     * 修改备件使用记录管理
     *
     * @param usageRecords 备件使用记录管理
     * @return 结果
     */
    @Override
    public int updateUsageRecords(UsageRecords usageRecords) {
        usageRecords.setUpdateTime(DateUtils.getNowDate());
        return usageRecordsMapper.updateUsageRecords(usageRecords);
    }

    /**
     * 批量删除备件使用记录管理
     *
     * @param recordIds 需要删除的备件使用记录管理主键
     * @return 结果
     */
    @Override
    public int deleteUsageRecordsByRecordIds(Long[] recordIds) {
        return usageRecordsMapper.deleteUsageRecordsByRecordIds(recordIds);
    }

    /**
     * 删除备件使用记录管理信息
     *
     * @param recordId 备件使用记录管理主键
     * @return 结果
     */
    @Override
    public int deleteUsageRecordsByRecordId(Long recordId) {
        return usageRecordsMapper.deleteUsageRecordsByRecordId(recordId);
    }
}
